package com.example.springbootmybatis.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springbootmybatis.common.RedisKeyConstant;
import com.example.springbootmybatis.mapper.MjMapper;
import com.example.springbootmybatis.pojo.SignalMjDhModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: springboot-mybatis
 * @description: 11
 * @author: XianJin.Lu
 * @create: 2021-09-09 16:07
 **/

@RestController
public class MjController {
    @Autowired
    MjMapper mjMapper;

    @Autowired
    StringRedisTemplate redisTemplate;

    @RequestMapping("/queryList")
    public  List<SignalMjDhModel>  queryList(){
        List<SignalMjDhModel> signalMjDhModels = mjMapper.queryMjList();

        for (SignalMjDhModel signalMjDhModel : signalMjDhModels) {

            //实体类转化成json字符串  作为hash的value
            String value = JSONObject.toJSONString(signalMjDhModel);
            System.out.println(value);

            String gatherId = signalMjDhModel.getGatherId();
            String gatherResId = signalMjDhModel.getGatherResId();
            String signalType = signalMjDhModel.getSignalType();
            String pointAddr = signalMjDhModel.getPointAddr();


            String key="MJ_"+gatherId+"_"+gatherResId+"_"+signalType+"_"+pointAddr ;

        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_MJ_SIGNAL);

            hashOps.put(key,value);
        }
        return signalMjDhModels;
    }
}
